//
//  ChatManager.swift
//  swiftUIdemo
//
//  Created by Z.cy on 2025/1/10.
//

import Foundation
import Combine
import UIKit
class ChatManager : ObservableObject{
    @Published var messages : [Message] = []
    @Published var keyboardShowing : Bool = false
    var user: User
    var keyboardPublisher: AnyCancellable? = nil
    init(user: User) {
        
        self.user = user
        loadMessages()
 
    }
    func loadMessages() {
        messages = [
              Message(content: "1"), Message(content: "2", user: User.Other1),
              Message(content: "3"), Message(content: "4", user: User.Other1),
              Message(content: "5"), Message(content: "6", user: User.Other1),
              Message(content: "7"), Message(content: "8", user: User.Other1),
              Message(content: "9"), Message(content: "10", user: User.Other1)
        ]
    }
    
    func setUpPublishers() {
        keyboardPublisher = Publishers.Merge(
            NotificationCenter
              .default
              .publisher(for: UIResponder.keyboardWillShowNotification)
              .map { _ in true },
            NotificationCenter
              .default
              .publisher(for: UIResponder.keyboardWillHideNotification)
              .map { _ in false })
            .subscribe(on: DispatchQueue.main)
            .assign(to: \.keyboardShowing, on: self)
    }
    func sendMessage(_ message : Message){
        messages.append(message)
        
    }
    
}
